package com.shijiwei.lifthelper.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.shijiwei.lifthelper.R;
import com.shijiwei.lifthelper.fragment.CollectFragment;
import com.shijiwei.lifthelper.fragment.CookBookFragment;
import com.shijiwei.lifthelper.fragment.JokeFragment;
import com.shijiwei.lifthelper.fragment.MapFragment;
import com.shijiwei.lifthelper.fragment.OurFragment;
import com.shijiwei.lifthelper.fragment.WeatherFragment;
import com.shijiwei.lifthelper.utils.FragmentUtils;
import com.zbar.lib.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SlidingMenu menu;
    private RadioGroup group;
    private TextView title;
    private List<Fragment> fragments;
    private FragmentManager manager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        addLeftMenu();

        addFragments();

        addListener();



    }

    private void addListener() {
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menu.toggle();
            }
        });
    }

    private void addFragments() {
        fragments = new ArrayList<>();
        manager = getSupportFragmentManager();
        fragments.add(WeatherFragment.newInstance());
        fragments.add(JokeFragment.newInstance());
        fragments.add(CookBookFragment.newInstance());
        fragments.add(MapFragment.newInstance());
        fragments.add(CollectFragment.newInstance());
        fragments.add(OurFragment.newInstance());

        FragmentUtils utils = new FragmentUtils(fragments,group,manager,R.id.fl_container,menu);
    }

    public void addLeftMenu(){
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
//        menu.setShadowDrawable(R.drawable.shadow);

        // 设置滑动菜单视图的宽度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        menu.setFadeDegree(0.35f);

        //把滑动菜单添加进所有的Activity中，可选值SLIDING_CONTENT ， SLIDING_WINDOW
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        //为侧滑菜单设置布局
        menu.setMenu(R.layout.leftmenu);
        menu.showMenu();
        group = (RadioGroup) findViewById(R.id.rg_menu);
        title = (TextView) findViewById(R.id.tv_title);
        TextView code = (TextView) findViewById(R.id.code);
        code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,CodeResultActivity.class));
            }
        });

    }

    private long timeMillis = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (System.currentTimeMillis() - timeMillis > 2000){

                if (menu.isMenuShowing()){
                    finish();
                }else {
                    Toast.makeText(this, "请再按一次退出", Toast.LENGTH_SHORT).show();
                    menu.toggle();
                }
                timeMillis = System.currentTimeMillis();
            }else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
